﻿using System.Drawing;
using System.Windows.Forms;
using XDAFacebook;
using XFControls.Utils;
using FBEntities.FBObjects;

namespace XFControls.XFPanels.XFPanelItems
{
    internal class XFItemGroupInfo : XFItemBase
    {
        private int _categoryHeight = 30;
        private int _height;
        private int _indent = 15;
        private Size _mesTitle;
        private int _singleLineText = 30;
        private FBGroup _group;

        private Rectangle _description;

        public XFItemGroupInfo()
        {
            _height = -1;
            _mesTitle = new Size(-1, -1);

            Style = new XFItemStyle
                        {
                            BackgroundColor = Color.White,
                            TextFont = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular),
                            TextColor = Color.DarkSlateGray,
                            Padding = 10
                        };

            if (XFGraphics.GetScreenResolutionNotRotated() == ScreenResolution.QVGA)
            {
                _categoryHeight /= 2;
                _singleLineText /= 2;
                _indent /= 2;
            }
            ItemType = XFItemType.Display;
        }

        public FBGroup Group
        {
            get { return _group; }
            set { _group = value; }
        }

        public override int GetHeight()
        {
            if (_height > 0)
                return _height;

            int width = Screen.PrimaryScreen.WorkingArea.Width;
            int curY = Style.PaddingTop;

            determinLongest();

            curY = getCategoryHeight(curY);

            curY = getLineTextHeight(curY);
            curY = getLineTextHeight(curY);
            curY = getLineTextHeight(curY);
            curY = getLineTextHeight(curY);

            curY = getParagraphTextHeight(width, Group.description, curY, ref _description);

            _height = curY;

            return _height;
        }

        public override void ResetHeight()
        {
            _height = -1;
        }

        public override void ItemPaint(Graphics gf, int x, int y)
        {
            base.ItemPaint(gf, x, y);
            int width = GetWidth();
            int xLocal = 0;

            int curY = 0;
            using (var pCat = new Pen(Color.MidnightBlue))
            using (var bCat = new SolidBrush(Color.LightSteelBlue))
            using (var fCat = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular))
            using (var bText = new SolidBrush(Color.Black))
            {
                curY = drawCategory(gf, width, Settings.LocalizationStrings.basicInfo, pCat, bCat, bText, fCat, xLocal, curY);
                curY = drawLineText(gf, width, Settings.LocalizationStrings.Owner + ":  " + Group.owner.name, bText, curY);
                curY = drawLineText(gf, width, Settings.LocalizationStrings.Version + ":  " + Group.version, bText, curY);
                curY = drawLineText(gf, width, Settings.LocalizationStrings.Privacy + ":  " + Group.privacy, bText, curY);
                drawLineText(gf, width, Settings.LocalizationStrings.Profile + ":  " + Group.link, bText, curY);
                drawParagraphText(gf, Group.description, Settings.LocalizationStrings.Description, bText, _description);
            }
        }

        private void determinLongest()
        {
            if (_mesTitle.Width > 0)
                return;

            string cur;
            Size curMes;

            cur = Settings.LocalizationStrings.Owner;
            curMes = Style.TextFont.MeasureString(cur, new Rectangle(0, 0, 400, 0));
            if (_mesTitle.Width < curMes.Width)
                _mesTitle = curMes;
            cur = Settings.LocalizationStrings.Version;
            curMes = Style.TextFont.MeasureString(cur, new Rectangle(0, 0, 400, 0));
            if (_mesTitle.Width < curMes.Width)
                _mesTitle = curMes;
            cur = Settings.LocalizationStrings.Privacy;
            curMes = Style.TextFont.MeasureString(cur, new Rectangle(0, 0, 400, 0));
            if (_mesTitle.Width < curMes.Width)
                _mesTitle = curMes;
            cur = Settings.LocalizationStrings.Profile;
            curMes = Style.TextFont.MeasureString(cur, new Rectangle(0, 0, 400, 0));
            if (_mesTitle.Width < curMes.Width)
                _mesTitle = curMes;
            cur = Settings.LocalizationStrings.Description;
            curMes = Style.TextFont.MeasureString(cur, new Rectangle(0, 0, 400, 0));
            if (_mesTitle.Width < curMes.Width)
                _mesTitle = curMes;
            cur = Settings.LocalizationStrings.Books;
            curMes = Style.TextFont.MeasureString(cur, new Rectangle(0, 0, 400, 0));
            if (_mesTitle.Width < curMes.Width)
                _mesTitle = curMes;
            cur = Settings.LocalizationStrings.Quotes;
            curMes = Style.TextFont.MeasureString(cur, new Rectangle(0, 0, 400, 0));
            if (_mesTitle.Width < curMes.Width)
                _mesTitle = curMes;
            cur = Settings.LocalizationStrings.Bio;
            curMes = Style.TextFont.MeasureString(cur, new Rectangle(0, 0, 400, 0));
            if (_mesTitle.Width < curMes.Width)
                _mesTitle = curMes;

            _mesTitle.Width += _indent;
        }

        private int drawLineText(Graphics g, int width, string text, SolidBrush bText, int curY)
        {
            if (Style == null) return curY + _singleLineText;
            g.DrawString(XFControlUtils.EllipsisWord(width - (_indent * 2), text, Style.TextFont), Style.TextFont, bText,
                         _indent, curY);
            return curY + _singleLineText;
        }

        private int getLineTextHeight(int curY)
        {
            return curY + _singleLineText;
        }

        private void drawParagraphText(Graphics g, string text, string title, SolidBrush bText, Rectangle rec)
        {
            title = title + ":  ";
            if (rec.Y <= 0) return;

            g.DrawString(title, Style.TextFont, bText, _indent, rec.Y);
            g.DrawString(text, rec, Style, bText.Color);
        }

        private int getParagraphTextHeight(int width, string text, int curY, ref Rectangle output)
        {
            int titleWidth = _mesTitle.Width;
            output = new Rectangle(_indent + titleWidth, curY, width - (_indent * 2) - titleWidth, 0);

            if (string.IsNullOrEmpty(text))
                return curY + _singleLineText;

            int recHeight = Style.TextFont.MeasureString(text, output).Height;
            output.Height = recHeight;

            return curY + recHeight + Style.PaddingTop;
        }

        private int drawCategory(Graphics g, int width, string title, Pen pCat, SolidBrush bCat, SolidBrush bText,
                                 Font fCat, int x, int curY)
        {
            int ret = curY + _categoryHeight + Style.PaddingTop;

            g.FillRectangle(bCat, x, curY, width, _categoryHeight);
            g.DrawLine(pCat, x, curY, width, curY);

            g.DrawString(title, fCat, bText, x + Style.PaddingLeft, curY + 3);

            return ret;
        }

        private int getCategoryHeight(int curY)
        {
            return curY + _categoryHeight + (Style.PaddingLeft + Style.PaddingRight);
        }
    }
}